package com.android.baseconfig.base;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.baseconfig.R;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import butterknife.ButterKnife;

/**
 * @author : Administrator.
 *         Created time: 2016-10-25.
 *         Desc:
 */
public abstract class BaseFragment<V extends BaseView, T extends BasePresenter<V>> extends Fragment {

    protected T presenter;

    protected BaseActivity activity;
    protected View view;

    protected Bundle savedInstanceState;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.savedInstanceState = savedInstanceState;
        activity = (BaseActivity) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(resourceId(), container, false);
        if (view == null) {
            throw new IllegalArgumentException("Fragment should implement initView method and cannot return null");
        }
        ButterKnife.bind(this, view);
        initPresenter();
        initData();
        initEvent();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (presenter != null) {
            presenter.destroy();
            presenter.detachView();
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    protected abstract int resourceId();

    protected abstract T createPresenter();

    protected void initData() {
    }

    protected void initEvent() {
    }

    /*public void showLoadingView() {
        if (mActivity != null && !mActivity.isFinishing()) {
            mActivity.showLoadingView();
        }
    }

    public void showLoadingDialog(String title) {
        if (mActivity != null && !mActivity.isFinishing()) {
            mActivity.showLoadingView(title);
        }
    }

    public void hideLoadingView() {
        if (mActivity != null && !mActivity.isFinishing()) {
            mActivity.hideLoadingView();
        }
    }*/


    // public <E> Observable.Transformer<E, E> getTransformer() {
    //     return this.bindToLifecycle();
    // }

    public T getPresenter() {
        return presenter;
    }

    private void initPresenter() {
        presenter = createPresenter();
        if (presenter != null) {
            presenter.attachView((V) this);
            presenter.init();
            presenter.start();
        }
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        activity.overridePendingTransition(R.anim.activity_open_enter, R.anim.activity_open_exit);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        activity.overridePendingTransition(R.anim.activity_open_enter, R.anim.activity_open_exit);
    }

}
